package com.example.administrator.daggerdemo.presenter;


import com.example.administrator.daggerdemo.base2.base.BaseObserver;
import com.example.administrator.daggerdemo.base2.base.BasePresenterImpl;

import com.example.administrator.daggerdemo.model.network.request.LoginRequestInfo;
import com.example.administrator.daggerdemo.model.network.response.LoginResponseInfo;
import com.example.administrator.daggerdemo.utils.PhoneUtils;
import com.example.administrator.daggerdemo.utils.StringUtils;
import com.example.administrator.daggerdemo.view.IMainActivityView;


import org.json.JSONException;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 *
 * Created by zhengjian on 2016/12/8.
 */

public class MainActivityPresenter extends BasePresenterImpl<IMainActivityView> {


    /**
     * 构造方法
     *
     * @param view 具体业务的接口对象
     */
    public MainActivityPresenter(IMainActivityView view) {
        super(view);
    }

    /**
     *登录方法
     * @param userName 用户名
     * @param userPWD 用户密码
     */
    public void doLogin(String userName,String userPWD)
    {
        try {
            final LoginRequestInfo loginRequestInfo = new LoginRequestInfo();
            loginRequestInfo.setApp_version(PhoneUtils.getAPPVersion());
            loginRequestInfo.setDevice_token(PhoneUtils.getDeviceToken());
            loginRequestInfo.setPhone_model(PhoneUtils.getPhoneModel());
            loginRequestInfo.setApp_id(PhoneUtils.getApp_id());
            loginRequestInfo.setPhone_version(PhoneUtils.getAndroidVersion());
            loginRequestInfo.setUser_name(userName);
            loginRequestInfo.setUser_password(StringUtils.MD5(userPWD));
            iView.showProgress();
            Subscription subscription   = netWorkManger.doLogin(loginRequestInfo).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<LoginResponseInfo>() {
                        @Override
                        public void onCompleted() {
                          iView.hideProgress();
                        }

                        @Override
                        public void onFail(String failMsg) {
                            iView.toast(failMsg);
                        }

                        @Override
                        public void onFail(int failId) {
                             switch (failId)
                             {
                                 case 0:
                                     iView.msgError("账号或密码错误");
                                     break;
                             }
                        }

                        @Override
                        public void onSuccess(LoginResponseInfo bean) {
                              iView.loginSuccess(bean);
                        }
                    });
            addSubscription(subscription);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }






}
